package com.keter.framework.web.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.keter.framework.core.exception.ValidateException;
import com.keter.framework.core.result.JSONResult;
import org.apache.commons.lang3.Validate;

import java.util.List;

/**
 * 集合操作工具
 */
public class WrapUtil {

    static protected boolean jsonKitAvailable;

    static {
        // Is JsonKit Available?
        try {
            jsonKitAvailable = null != Class.forName("com.jfinal.kit.JsonKit");
        } catch (Throwable t) {
            jsonKitAvailable = false;
        }
    }

    public static JSONResult wrapObject(Object obj) {
        if (obj == null) {
//            return wrap(new JSONObject());
            return JSONResult.emptyResult();
        }

        if (obj instanceof String) {
            return wrap((String) obj);
        }
        if (obj instanceof Long) {
            return wrap((Long) obj);
        }
        if (obj instanceof Boolean) {
            return wrap((Boolean) obj);
        }
        if (obj instanceof JSONObject) {
            return wrap((JSONObject) obj);
        }
        if (obj instanceof List) {
            return wrap((List) obj);
        }
        if (obj instanceof Model) {
            return wrap((Model) obj);
        }
        if (obj instanceof Record) {
            return wrap((Record) obj);
        }
        throw new ValidateException("不支持的结果集类型!");
    }

    public static JSONResult wrap(String content) {
        JSONResult result = new JSONResult();
        result.put(JSONResult.KEY_STATUS, JSONResult.Status.OK.value());
        result.put(JSONResult.KEY_DATA, content);
        return result;
    }

    public static JSONResult wrap(Long content) {
        JSONResult result = new JSONResult();
        result.put(JSONResult.KEY_STATUS, JSONResult.Status.OK.value());
        result.put(JSONResult.KEY_DATA, content);
        return result;
    }

    public static JSONResult wrap(boolean content) {
        return wrap(String.valueOf(content));
    }

    public static JSONResult wrap(JSONObject jsonObject) {
        Validate.isTrue(!(jsonObject instanceof JSONResult), "不能对JSONResult对象进行包装！");
        if(jsonObject==null || jsonObject.isEmpty()){
            return JSONResult.emptyResult();
        }
        JSONResult result = new JSONResult();
        result.put(JSONResult.KEY_STATUS, JSONResult.Status.OK.value());
        result.put(JSONResult.KEY_DATA, jsonObject);
        return result;
    }

    public static JSONResult wrap(List list) {
        JSONResult result = new JSONResult();
        result.put(JSONResult.KEY_STATUS, JSONResult.Status.OK.value());
        if (list == null || list.size() == 0) {
            result.put(JSONResult.KEY_DATA, new JSONArray());
        } else {
            if (jsonKitAvailable) {
                result.put(JSONResult.KEY_DATA, JSONObject.parseArray(JsonKit.toJson(list)));
            } else {
                result.put(JSONResult.KEY_DATA, JSONObject.parseArray(JSON.toJSONString(list)));
            }
        }
        return result;
    }

    public static JSONResult wrap(Model content) {
        if (content == null) {
            return JSONResult.emptyResult();
        }
        return WrapUtil.wrap(JSONObject.parseObject(content.toJson()));
    }

//    private static JSONResult emptyResult() {
//        return JSONResult.emptyResult();
//    }

    public static JSONResult wrap(Record record) {
        if (record == null) {
            return JSONResult.emptyResult();
        }
        return WrapUtil.wrap(JSONObject.parseObject(record.toJson()));
    }
}
